package com.easysoft.framework.action;

import com.easysoft.commons.utils.FileUtils;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.friendlink.model.FriendLinkQuery;
import com.easysoft.framework.friendlink.model.FriendLinkView;
import com.easysoft.framework.friendlink.model.SysFriendLink;
import com.easysoft.framework.friendlink.service.FriendLinkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/7/5.
 */

@RestController
@RequestMapping(value = SystemFriendAction.BASE_VIEW)
public class SystemFriendAction {
    public static final String BASE_VIEW = "/friend";
    private final static Logger logger = LoggerFactory.getLogger(SystemFriendAction.class);

    @Autowired
    @Qualifier("service.FriendLinkService")
    private FriendLinkService friendURLService;

    /**
     * 分页查询友情链接
     *
     * @param page  分页
     * @param query 查询条件
     * @return 友情链接列表
     */
    @RequestMapping(value = "/listFriendUrl", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listFriendUrl(Page page, FriendLinkQuery query) {

        logger.info("Call SystemFriendAction.listFriendUrl({},{})", page.toString(), query.toString());

        List<FriendLinkView> listFriendUrl = friendURLService.selectSysFriendListByPage(page, query);

        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listFriendLink");
        mv.addObject("page", page); // 返回分页
        mv.addObject("query", query);// 查询条件
        mv.addObject("listFriendUrl", listFriendUrl); // 返回查询结果集
        return mv;
    }

    @RequestMapping(value = "/editFriendUrl", method = RequestMethod.GET)
    public ModelAndView editFriendUrl(String action, @RequestParam(required = false) String id) {
        logger.info("Call SystemFriendAction.editFriendUrl({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editFriendLink");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysFriendLink entity = friendURLService.selectFriendLinkById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveFriendUrl", method = RequestMethod.POST)
    public int saveFriendUrl(String action, SysFriendLink friendURL, @RequestParam(value="multipartFile", required=false)MultipartFile multipartFile, HttpServletRequest request, HttpSession session) {
        logger.info("Call SystemManagerAction.saveManager({},{})", action, friendURL.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(friendURL.getId())) {
                return 0;
            }
            return addFriendUrl(friendURL, multipartFile, request,session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(friendURL.getId())) {
                return 0;
            }
            return updateFriendUrl(friendURL, multipartFile, request, session);
        }
        return 0;
    }

    /**
     * 添加友情链接
     *
     * @param friendURL 友情链接表单信息
     * @return 1 表示成功，0 表示失败
     */
    @SystemControllerLog(description = "添加友情链接")
    private int addFriendUrl(SysFriendLink friendURL, @RequestParam(value="multipartFile", required=false)MultipartFile multipartFile,HttpServletRequest request,HttpSession session) {
        logger.info("Call SystemFriendAction.addFriendUrl({})", friendURL.toString());

        if (StringUtils.isNullOrBlank(friendURL.getSiteName()) || StringUtils.isNullOrBlank(friendURL.getSiteUrl())) {
            // 链接名或链接地址为空
            return 0;
        }

        if(multipartFile != null && !("").equals(multipartFile)){
            friendURL.setSiteLogo( FileUtils.savePicture(multipartFile,"friend",request));
        }

        friendURL.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return friendURLService.addFriendLink(friendURL);

    }

    /**
     * 修改友情链接
     *
     * @param friendURL 友情链接表单信息
     * @return 1 表示成功，0 表示失败
     */
    private int updateFriendUrl(SysFriendLink friendURL, @RequestParam(value="multipartFile", required=false)MultipartFile multipartFile,HttpServletRequest request,HttpSession session) {
        logger.info("Call SystemFriendAction.updateFriendUrl({})", friendURL.toString());

        SysFriendLink temp = friendURLService.selectFriendLinkById(friendURL.getId());
        if (temp == null) {
            // 修改的记录不存在
            return 0;
        }

        if(multipartFile != null && !("").equals(multipartFile)){
            friendURL.setSiteLogo( FileUtils.savePicture(multipartFile,"friend",request));
        }

        friendURL.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return friendURLService.updateFriendLink(friendURL);

    }

    /**
     * 删除友情链接
     *
     * @param id 编号
     * @return 1 表示成功，0 表示失败
     */
    @RequestMapping(value = "deleteFriendUrl", method = RequestMethod.POST)
    public int deleteFriendUrl(String id) {
        logger.info("Call SystemFriendAction.deleteFriendUrl({})", id);

        SysFriendLink friendURL = friendURLService.selectFriendLinkById(id);
        if (friendURL == null) {
            // 记录不存在
            return 0;
        }
        return friendURLService.deleteFriendLink(id);

    }
}
